package com.sendi.mobilework.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * Created by Administrator on 2017/3/25.
 */

public class PinyinUtils {

    /**
     * 获取汉语拼音首字母
     * @param chines 汉字
     * @return 拼音
     */
    public static String getAlpha(String chines){
        String pinyinName="";
        char[] nameChar=chines.toCharArray();
        HanyuPinyinOutputFormat defaultFormat=new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i=0;i<nameChar.length;i++){
            if (nameChar[i]>128){
                try {
                    pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i],defaultFormat)[0].charAt(0);
                } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                    badHanyuPinyinOutputFormatCombination.printStackTrace();
                }
            }else {
                pinyinName +=nameChar[i];
            }
        }
        return pinyinName;
    }

    /**
     * 将字符串中的中文转换为拼音，英文字符串不变
     * @param inputString 汉字
     * @return 拼音
     */
    public static String getPinYin(String inputString){
        HanyuPinyinOutputFormat format=new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        String output="";
        if (inputString!=null&&inputString.length()>0
                &&!"null".equals(inputString)){
            char[] input=inputString.trim().toCharArray();
            for (int i=0;i<input.length;i++){
                if (Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")){
                    try {
                        String[] temp= PinyinHelper.toHanyuPinyinStringArray(input[i],format);
                        output +=temp[0];
                    } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                        badHanyuPinyinOutputFormatCombination.printStackTrace();
                    }
                } else {
                    output +=Character.toString(input[i]);
                }
            }
        }else {
            return "*";
        }
        return output;
    }

    /**
     * 阿红娘子转换为汉语拼音首字母，英文字符不变
     * @param chines 汉字
     * @return 拼音
     */
    public static String converterToFirstSpell(String chines){
        String pinyinName="";
        char[] nameChar=chines.toCharArray();
        HanyuPinyinOutputFormat defaultFormat=new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i=0;i<nameChar.length;i++){
            if (nameChar[i]>128){
                try {
                    pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i],
                            defaultFormat)[0].charAt(0);
                } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                    badHanyuPinyinOutputFormatCombination.printStackTrace();
                }
            }else {
                pinyinName+=nameChar[i];
            }
        }
        return pinyinName;
    }
}
